home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / program / asmtext1.zip / ADDRESS.TXT < prev   
Text File  |  1994-02-27  |  11KB  |  240 lines

  1. Effective Addresses
  2.  
  3. Most memory data accessing in the 86 family is accomplished via
  4. the mechanism of the effective address.  Wherever an effective
  5. address specifier "eb", "ew" or "ed" appears in the list of 8086
  6. instructions, you may use a wide variety of actual operands in
  7. that instruction.  These include general registers, memory
  8. variables, and a variety of indexed memory quantities.
  9.  
  10. GENERAL REGISTERS: Wherever an "ew" appears, you can use any of
  11. the 16-bit registers AX,BX,CX,DX,SI,DI,SP, or BP.  Wherever an
  12. "eb" appears, you can use any of the 8-bit registers
  13. AL,BL,CL,DL,AH,BH,CH, or DH.  For example, the "ADD ew,rw" form
  14. subsumes the 16-bit register-to-register adds; for example, ADD
  15. AX,BX; ADD SI,BP; ADD SP,AX.
  16.  
  17. MEMORY VARIABLES: Wherever an "ew" appears, you can use a word
  18. memory variable.  Wherever an "eb" appears, you can use a byte
  19. memory variable.  Variables are typically declared in the DATA
  20. segment, using a DW declaration for a word variable, or a DB
  21. declaration for a byte variable.  For example, you can declare
  22. variables:
  23.  
  24.      DATA_PTR  DW ?
  25.      ESC_CHAR  DB ?
  26.  
  27. Later, you can load or store these variables:
  28.  
  29.      MOV SI,DATA_PTR    ; load DATA_PTR into SI for use
  30.      LODSW              ; fetch the word pointed to by DATA_PTR
  31.      MOV DATA_PTR,SI    ; store the value incremented by the LODSW
  32.      MOV BL,ESC_CHAR    ; load the byte variable ESC_CHAR
  33.  
  34. Alternatively, you can address specific unnamed memory locations
  35. by enclosing the location value in square brackets; for example,
  36.  
  37.      MOV AL,[02000]     ; load contents of location 02000 into AL
  38.  
  39. Sometimes you must specify byte or word:
  40.  
  41.      INC B[02000]       ; increment the byte at location 02000
  42.      MOV W[02000],0     ; set the WORD at location 02000 to zero
  43.  
  44. INDEXED MEMORY: The 86 supports the use of certain registers as
  45. base pointers and index registers into memory.  BX and BP are the
  46. base registers; SI and DI are the index registers.  You may
  47. combine at most one base register, at most one index register,
  48. and a constant number into a run time pointer that determines the
  49. location of the effective address memory to be used in the
  50. instruction.  These can be given explicitly, by enclosing the
  51. index registers in brackets:
  52.  
  53.      MOV AX,[BX]
  54.      MOV CX,W[SI+17]
  55.      MOV AX,[BX+SI+5]
  56.      MOV AX,[BX][SI]5   ; another way to write the same instr.
  57.  
  58. Or, indexing can be accomplished by declaring variables in a
  59. based structure type:
  60.  
  61.      STRUC [BP]        ; NOTE: based structures are unique to some assemblers
  62.        BP_SAVE   DW ?  ; BP_SAVE is a word at [BP]
  63.        RET_ADDR  DW ?  ; RET_ADDR is a word at [BP+2]
  64.        PARM1     DW ?  ; PARM1 is a word at [BP+4]
  65.        PARM2     DW ?  ; PARM2 is a word at [BP+6]
  66.      ENDS
  67.      INC PARM1         ; equivalent to INC W[BP+4]
  68.  
  69. Finally, indexing can be done by mixing explicit components with
  70. declared ones:
  71.  
  72.      TABLE    DB  4,2,1,3,5
  73.      MOV AL,TABLE[BX]        ; load byte number BX of TABLE
  74.  
  75.  
  76. Segmentation and Effective Addresses
  77.  
  78. The 86 family has four segment registers, CS, DS, ES, and SS,
  79. used to address memory.  Each segment register points to 64K
  80. bytes of memory within the 1-megabyte memory space of the 86.
  81. (The start of the 64K is calculated by multiplying the segment
  82. register value by 16; i.e., by shifting the value left by one hex
  83. digit.)  If your program's code, data and stack areas can all fit
  84. in the same 64K bytes, you can leave all the segment registers
  85. set to the same value.  In that case, you won't have to think
  86. about segment registers--no matter which one is used to address
  87. memory, you'll still get the same 64K.  If your program needs
  88. more than 64K, you must point one or more segment registers to
  89. other parts of the memory space.  In this case, you must take
  90. care that your memory references use the segment registers you
  91. intended.
  92.  
  93. Each effective address memory access has a default segment
  94. register, to be used if you do not explicitly specify which
  95. segment register you wish.  For most effective addresses, the
  96. default segment register is DS.  The exceptions are those
  97. effective addresses that use the BP register for indexing.  All
  98. BP-indexed memory references have a default of SS.  (This is
  99. because BP is intended to be used for addressing local variables,
  100. stored on the stack.)
  101.  
  102. If you wish your memory access to use a different segment
  103. register, you provide a segment override byte before the
  104. instruction containing the effective address operand.
  105.  
  106. Effective Use of Effective Addresses
  107.  
  108. Remember that all of the common instructions of the 86 family
  109. allow effective addresses as operands.  (The only major functions
  110. that don't are the AL/AX specific ones: multiply, divide, and
  111. input/output).  This means that you don't have to funnel many
  112. through AL or AX just to do something with them.  You can perform
  113. all the common arithmetic, PUSH/POP, and MOVes from any general
  114. register to any general register; from any memory location
  115. (indexed if you like) to any register; and (this is most often
  116. overlooked) from any register TO memory.  The only thing you
  117. can't do in general is memory-to-memory.  Among the more common
  118. operations that inexperienced 86 programmers overlook are:
  119.  
  120.    * setting memory variables to immediate values
  121.  
  122.    * testing memory variables, and comparing them to constants
  123.  
  124.    * preserving memory variables by PUSHing and POPping them
  125.  
  126.    * incrementing and decrementing memory variables
  127.  
  128.    * adding into memory variables
  129.  
  130. Encoding of Effective Addresses
  131.  
  132. Unless you are concerned with the nitty-gritty details of 86
  133. instruction encoding, you don't need to read this section.
  134.  
  135. Every instruction with an effective address has an encoded byte,
  136. known as the effective address byte, following the 1-byte opcode
  137. for the instruction. (For obscure reasons, Intel calls this byte
  138. the ModRM byte.)  If the effective address is a memory variable,
  139. or an indexed memory location with a non-zero constant offset,
  140. then the effective address byte will be immediately followed by
  141. the offset amount.  Amounts in the range -128 to +127 are given
  142. by a single signed byte, denoted by "d8" in the table below.
  143. Amounts requiring a 2-byte representation are denoted by "d16" in
  144. the table below.  As with all 16-bit memory quantities in the 86
  145. family, the word is stored with the least significant byte FIRST.
  146.  
  147. The following table of effective address byte values is organized
  148. into 32 rows and 8 columns.  The 32 rows give the possible values
  149. for the effective address operand: 8 registers and 24 memory
  150. indexing modes.  A 25th indexing mode, [BP] with zero
  151. displacement, has been pre-empted by the simple-memory-variable
  152. case.  If you code [BP] with no displacement, you will get
  153. [BP]+d8, with a d8-value of zero.
  154.  
  155. The 8 columns of the table reflect further information given by
  156. the effective address byte.  Usually, this is the identity of the
  157. other (always a register) operand of a 2-operand instruction.
  158. Those instructions are identified by a "/r" following the opcode
  159. byte in the instruction list.  Sometimes, the information given
  160. supplements the opcode byte in identifying the instruction
  161. itself.  Those instructions are identified by a "/" followed by a
  162. digit from 0 through 7.  The digit tells which of the 8 columns
  163. you should use to find the effective address byte.
  164.  
  165. For example, suppose you have a perverse wish to know the precise
  166. bytes encoded by the instruction SUB B[BX+17],100.  This
  167. instruction subtracts an immediate quantity, 100, from an
  168. effective address quantity, B[BX+17].  By consulting the
  169. instruction list, you find the general form SUB eb,ib.  The
  170. opcode bytes given there are 80 /5 ib.  The "/5" denotes an
  171. effective address byte, whose value will be taken from column 5
  172. of the following table.  The offset 17 decimal, which is 11 hex,
  173. will fit in a single "d8" byte, so we take our value from the
  174. "[BX] + d8" row.  The table tells us that the effective address
  175. byte is 6F.  Immediately following the 6F is the offset, 11 hex.
  176. Following that is the ib-value of 100 decimal, which is 64 hex.
  177. So the bytes generated by SUB B[BX+17],100 are 80 6F 11 64.
  178.  
  179.  
  180.  
  181. Table of Effective Address byte values
  182.  
  183. s  =     ES   CS   SS   DS
  184. rb =     AL   CL   DL   BL   AH   CH   DH   BH
  185. rw =     AX   CX   DX   BX   SP   BP   SI   DI
  186. digit=    0    1    2    3    4    5    6    7
  187.                                                   Effective
  188. EA byte                                           address:
  189. values:  00   08   10   18   20   28   30   38    [BX + SI]
  190.          01   09   11   19   21   29   31   39    [BX + DI]
  191.          02   0A   12   1A   22   2A   32   3A    [BP + SI]
  192.          03   0B   13   1B   23   2B   33   3B    [BP + DI]
  193.  
  194.          04   0C   14   1C   24   2C   34   3C    [SI]
  195.          05   0D   15   1D   25   2D   35   3D    [DI]
  196.          06   0E   16   1E   26   2E   36   3E    d16 (simple var)
  197.          07   0F   17   1F   27   2F   37   3F    [BX]
  198.  
  199.          40   48   50   58   60   68   70   78    [BX + SI] + d8
  200.          41   49   51   59   61   69   71   79    [BX + DI] + d8
  201.          42   4A   52   5A   62   6A   72   7A    [BP + SI] + d8
  202.          43   4B   53   5B   63   6B   73   7B    [BP + DI] + d8
  203.  
  204.          44   4C   54   5C   64   6C   74   7C    [SI] + d8
  205.          45   4D   55   5D   65   6D   75   7D    [DI] + d8
  206.          46   4E   56   5E   66   6E   76   7E    [BP] + d8
  207.          47   4F   57   5F   67   6F   77   7F    [BX] + d8
  208.  
  209.          80   88   90   98   A0   A8   B0   B8    [BX + SI] + d16
  210.          81   89   91   99   A1   A9   B1   B9    [BX + DI] + d16
  211.          82   8A   92   9A   A2   AA   B2   BA    [BP + SI] + d16
  212.          83   8B   93   9B   A3   AB   B3   BB    [BP + DI] + d16
  213.  
  214.          84   8C   94   9C   A4   AC   B4   BC    [SI] + d16
  215.          85   8D   95   9D   A5   AD   B5   BD    [DI] + d16
  216.          86   8E   96   9E   A6   AE   B6   BE    [BP] + d16
  217.          87   8F   97   9F   A7   AF   B7   BF    [BX] + d16
  218.  
  219.          C0   C8   D0   D8   E0   E8   F0   F8    ew=AX   eb=AL
  220.          C1   C9   D1   D9   E1   E9   F1   F9    ew=CX   eb=CL
  221.          C2   CA   D2   DA   E2   EA   F2   FA    ew=DX   eb=DL
  222.          C3   CB   D3   DB   E3   EB   F3   FB    ew=BX   eb=BL
  223.  
  224.          C4   CC   D4   DC   E4   EC   F4   FC    ew=SP   eb=AH
  225.          C5   CD   D5   DD   E5   ED   F5   FD    ew=BP   eb=CH
  226.          C6   CE   D6   DE   E6   EE   F6   FE    ew=SI   eb=DH
  227.          C7   CF   D7   DF   E7   EF   F7   FF    ew=DI   eb=BH
  228.  
  229. d8 denotes an 8-bit displacement following the EA byte, to be
  230. sign-extended and added to the index.
  231.  
  232. d16 denotes a 16-bit displacement following the EA byte, to be
  233. added to the index.
  234.  
  235. Default segment register is SS for effective addresses containing
  236. a BP index; DS for other memory effective addresses.
  237.  
  238.  
  239.  
  240.